package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private DishFlavorServiceImpl dishFlavorService;


    @Override
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> voPage = new Page<>();
        Page<Dish> page1 = new Page(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dishPageQueryDTO.getName() != null, Dish::getName, dishPageQueryDTO.getName())
                .eq(dishPageQueryDTO.getStatus() != null, Dish::getStatus, dishPageQueryDTO.getStatus())
                .eq(dishPageQueryDTO.getCategoryId() != null, Dish::getCategoryId, dishPageQueryDTO.getCategoryId())
                .orderByDesc(Dish::getCreateTime);
        page1 = this.page(page1, queryWrapper);

        BeanUtils.copyProperties(page1, voPage, "records");
        LambdaQueryWrapper<Category> queryWrapper1 = new LambdaQueryWrapper<>();
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish dish : page1.getRecords()) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            Category category = categoryService.getById(dish.getCategoryId());
            if (category != null) {
                dishVO.setCategoryName(category.getName());
            }
            dishVOList.add(dishVO);
        }
        return new PageResult(this.count(), dishVOList);
    }

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.ENABLE);
        this.save(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        for (DishFlavor flavor : flavors) {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setName(flavor.getName());
            dishFlavor.setValue(flavor.getValue());
            dishFlavor.setDishId(dishId);
            dishFlavorService.save(dishFlavor);
        }
    }

    @Override
    @Transactional
    public void delDishWithDishFlavor(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);
        this.remove(queryWrapper);

        LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(queryWrapper1);

    }

    @Override
    public DishVO getDishAndDishflavorById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishVO);
        Category category = categoryService.getById(dish.getCategoryId());
        dishVO.setCategoryName(category.getName());
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateDishAndDishFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        LambdaUpdateWrapper<Dish> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Dish::getId, dishDTO.getId());
        this.update(dish, lambdaUpdateWrapper);

        LambdaUpdateWrapper<DishFlavor> lambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper1.eq(DishFlavor::getDishId, dishDTO.getId());
        dishFlavorService.remove(lambdaUpdateWrapper1);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setValue(flavor.getValue());
            dishFlavor.setName(flavor.getName());
            dishFlavor.setDishId(dishDTO.getId());
            dishFlavorService.save(dishFlavor);
        }

    }

    @Override
    public List<DishVO> getDishWithDishFlavor(int categoryId) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, categoryId);
        List<Dish> list = this.list(queryWrapper);
        List<DishVO> voList = new ArrayList<>();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
        for (Dish dish : list) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(dish.getId() != null, DishFlavor::getDishId, dish.getId());
            List<DishFlavor> flavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishVO.setFlavors(flavorList);
            dishVO.setCategoryName(categoryName);
            voList.add(dishVO);
        }
        return voList;
    }

    @Override
    public DishOverViewVO queryOverviewDishes() {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getStatus, StatusConstant.ENABLE);
        Integer sold = (int) this.count(dishLambdaQueryWrapper);

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getStatus, StatusConstant.DISABLE);
        Integer discontinued = (int) this.count(lambdaQueryWrapper);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
